package edu.whu.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Aspect
@Component
public class CountAspect {

    public static Map<String,Integer> map = new HashMap<>();

    @Pointcut("execution(* edu.whu.service.*.*(..))")
    private void pointCut(){
    }

    @Before("pointCut()")
    private void countInvoke(JoinPoint jp){
        String name = jp.getSignature().toString();
        if(map.containsKey(name)){
            map.put(name,map.get(name) + 1);
        }
        else {
            map.put(name,1);
        }
    }

    @AfterThrowing(pointcut= "pointCut()",throwing="ex")
    private void countException(JoinPoint jp, Exception ex) {
        String name = ex.getClass().toString();
        if(map.containsKey(name)){
            map.put(name,map.get(name) + 1);
        }
        else {
            map.put(name,1);
        }
    }
}
